package com.gxa.saas.service.impl;

import com.gxa.saas.mapper.BrandMapper;
import com.gxa.saas.mapper.BrandSecondMapper;
import com.gxa.saas.pojo.BrandFirst;
import com.gxa.saas.pojo.BrandSecond;
import com.gxa.saas.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * @author CZJ
 * @date 2022/6/7 15:22
 */
@Service
@Transactional(rollbackFor = {Throwable.class})

public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    BrandSecondMapper brandSecondMapper;
    @Override
    public List<Map<String, Object>> selectByPage(Integer limit, Integer size){
        return brandMapper.selectByPage((limit-1)*size, size);
    }

    @Override
    public Integer insert(BrandFirst brand) {
        List<BrandSecond> brandSeconds = brandSecondMapper.selectByFrom(brand.getId());
        brand.setCount(brandSeconds.size());
        return brandMapper.insert(brand);
    }

    @Override
    public List<BrandFirst> selectLike(String name,Integer page,Integer size) {

        return brandMapper.selectLike(name,page,size);
    }

    @Override
    public Integer update(BrandFirst brandFirst) {
        return brandMapper.update(brandFirst);
    }

    @Override
    public Integer delete(Integer id) {
        brandSecondMapper.deleteByFrom(id);
        return brandMapper.delete(id);
    }

    @Override
    public List<BrandFirst> selectAll() {
        return brandMapper.selectAll();
    }

    @Override
    public BrandFirst selectByName(String name) {
        return brandMapper.selectByName(name);
    }

    @Override
    public List<BrandFirst> selectBySort(Boolean boo) {
        if (boo){
            return brandMapper.ascendingOrder();
        }
        return brandMapper.descendingOrder();
    }
}
